package com.android.openapp.services;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

import com.android.openapp.R;
import com.android.openapp.data.Place;
import com.android.openapp.ui.Metainit;
import com.android.openapp.ui.Utils;

import com.socrata.util.Variables;


public class DownloadThread extends AsyncTask<String, Integer, Integer> {
	
	Context mContext;
	ProgressDialog pd;
	/**
	 * @author Allieri Tommaso
	 * classe che gestisce il download dei metadati
	 * per l'aggiunta di un nuovo dataset di metadati vedere la classe Location.
	 */
	public DownloadThread(Context context) {

		super();
		Metainit.flag =0;
		mContext = context;
		//pd = new ProgressDialog(context);
	}
	
	@Override
	protected Integer doInBackground(String... params) {
		//pd.setMessage(params[0]);
    	//Message message = new Message();
    	Log.i("LVUpdate/DOWNLOAD Thread", " *** Avvio della procedura di download ***  ");
    	//
    	for(Integer i=0;i<Place.datasetM.length;i++){
    		Log.d("datasetM",Place.datasetM[i]);
    		
    		Metainit t=new Metainit(i);
    		t.setPriority(Thread.MAX_PRIORITY-i);    		
    		t.start();
    		
    		Log.d("t","Thread partito"+t.getName());
    		
    		//publishProgress(i);

    	}
    	
    	while(Metainit.worked!=Place.datasetM.length){
    		//Log.i("DownloadThread","waiting");
    	}
    	return Metainit.flag;

	}
	
	@Override
	protected void onPreExecute() {
		//Inizilizzo l'arraylist dei metadati in Location
		Place.datasetM = mContext.getResources().getStringArray(R.array.metadata);
		Place.initialize();
		String title = mContext.getString(R.string.inizializzazione_title);
		String text = mContext.getString(R.string.inizializzazione_text);
		pd = ProgressDialog.show(mContext, title, text, true, false);
		super.onPreExecute();
	}
	
	@Override
	protected void onProgressUpdate(Integer...values) {
		pd.setMessage("MultiThread Metadata download Working"+Integer.toString(values[0]));
		super.onProgressUpdate(values);
	}

	@Override
	protected void onPostExecute(Integer result) {
		Intent i;
		if(result != Place.datasetM.length){
			pd.dismiss();
			Log.e("LVUpdate/UIActivity", "DOWNLOAD HAS RAISED AN ERROR");
			
			i = new Intent(Variables.DOWNLOAD_METADATA_FINISHED);
			i.putExtra(Variables.DOWNLOAD_RESULT, Variables.DOWNLOAD_ERROR);
			Utils.showToast(mContext, R.string.metadata_download_error);
			Metainit.worked=0;
    		Metainit.flag=0;

		}else{
			pd.dismiss();
			Log.d("DOWNLOAD FINISHED", "DOWNLOAD SUCCESFULLY COMPLETED!");
			
			i = new Intent(Variables.DOWNLOAD_METADATA_FINISHED);
			i.putExtra(Variables.DOWNLOAD_RESULT, Variables.NO_ERROR);
			Utils.showToast(mContext, R.string.metadata_download_ok);
			//createTabView();
			Metainit.worked=0;
    		Metainit.flag=0;
		}
		mContext.sendBroadcast(i);
		
		super.onPostExecute(result);
	}
	
}
